package cn.dm.item.controller;

import cn.dm.client.RestDmAreaClient;
import cn.dm.client.RestDmCinemaClient;
import cn.dm.client.RestDmImageClient;
import cn.dm.client.RestDmItemClient;
import cn.dm.common.DateUtil;
import cn.dm.common.Dto;
import cn.dm.common.DtoUtil;
import cn.dm.common.EmptyUtils;
import cn.dm.pojo.DmArea;
import cn.dm.pojo.DmCinema;
import cn.dm.pojo.DmImage;
import cn.dm.pojo.DmItem;
import cn.dm.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
public class ItemController {
    @Resource
    private RestDmItemClient restDmItemClient;
    @Resource
    private RestDmImageClient restDmImageClient;
    @Resource
    private RestDmAreaClient restDmAreaClient;
    @Resource
    private RestDmCinemaClient restDmCinemaClient;

    //查询banner轮播推荐的演出
    @PostMapping("/api/p/index/queryBanner")
    public Dto queryBanner() throws Exception {
        Map param = new HashMap();
        param.put("isBanner",1);
        param.put("beginPos",0);
        param.put("pageSize",5);

        List<DmItem> list = restDmItemClient.getDmItemListByMap(param);
        //需要把查询出来的数据，转换成view需要的数据
        List<HotItemVo> list2 = new ArrayList<HotItemVo>();
        if(EmptyUtils.isNotEmpty(list)){
            for(DmItem item : list){
                HotItemVo iv = new HotItemVo();
                BeanUtils.copyProperties(item,iv);//复制同名属性
                //获取该item的banner图
                List<DmImage> images = restDmImageClient.queryDmImageList(iv.getId(),1,1);
                if(EmptyUtils.isNotEmpty(images)) {
                    iv.setImgUrl(images.get(0).getImgUrl());
                }
                list2.add(iv);
            }
        }
        return DtoUtil.returnDataSuccess(list2);
    }

    //查询今日推荐
    @PostMapping("/api/p/index/queryTodayRecommend")
    public Dto queryTodayRecommend() throws Exception {
        Map param = new HashMap();
        param.put("isRecommend",1);
        param.put("beginPos",0);
        param.put("pageSize",6);

        List<DmItem> list = restDmItemClient.getDmItemListByMap(param);
        //需要把查询出来的数据，转换成view需要的数据
        List<HotItemVo> list2 = new ArrayList<HotItemVo>();
        if(EmptyUtils.isNotEmpty(list)){
            for(DmItem item : list){
                HotItemVo iv = new HotItemVo();
                BeanUtils.copyProperties(item,iv);//复制同名属性
                //获取该item的海报图
                List<DmImage> images = restDmImageClient.queryDmImageList(iv.getId(),2,1);
                if(EmptyUtils.isNotEmpty(images)) {
                    iv.setImgUrl(images.get(0).getImgUrl());
                }
                list2.add(iv);
            }
        }
        return DtoUtil.returnDataSuccess(list2);
    }

    //查询即将开售的演出
    @PostMapping("/api/p/index/queryToSaleItem")
    public Dto queryToSaleItem() throws Exception {
        Map param = new HashMap();

        Date now = new Date();
        Date tomorrow = DateUtil.addDay(now,1);

        param.put("minTime",DateUtil.format(now));
        param.put("maxTime",DateUtil.format(tomorrow));
        param.put("beginPos",0);
        param.put("pageSize",6);

        List<DmItem> list = restDmItemClient.getDmItemListByMap(param);
        //需要把查询出来的数据，转换成view需要的数据
        List<HotItemVo> list2 = new ArrayList<HotItemVo>();
        if(EmptyUtils.isNotEmpty(list)){
            for(DmItem item : list){
                HotItemVo iv = new HotItemVo();
                BeanUtils.copyProperties(item,iv);//复制同名属性
                //获取该item的海报图
                List<DmImage> images = restDmImageClient.queryDmImageList(iv.getId(),2,1);
                if(EmptyUtils.isNotEmpty(images)) {
                    iv.setImgUrl(images.get(0).getImgUrl());
                }
                list2.add(iv);
            }
        }
        return DtoUtil.returnDataSuccess(list2);
    }


    /**
     * 查询各个楼层（一级分类和其前5个热门演出）
     */
    @PostMapping("/api/p/index/queryFloorItems")
    public Dto queryFloorItems() throws Exception {
        List<DmFloorItems> list = restDmItemClient.queryItemByFloor();
        //给每个热门商品，设置图片的url
        if(EmptyUtils.isNotEmpty(list)){
            for (DmFloorItems vo: list) {
                if(EmptyUtils.isNotEmpty(vo.getItems())){
                    for(DmItemVo div : vo.getItems()){
                        List<DmImage> images = restDmImageClient.queryDmImageList(div.getId(),2,1);
                        if(EmptyUtils.isNotEmpty(images)) {
                            div.setImgUrl(images.get(0).getImgUrl());
                        }
                    }
                }
            }
        }
        return DtoUtil.returnDataSuccess(list);
    }



    @PostMapping("/api/p/index/queryHotItems")
    public Dto queryHotItems(@RequestBody Map<String,String> params) throws Exception {
        String itemTypeId = params.get("itemTypeId");

        Map param = new HashMap();

        Date now = new Date();
        Date tomorrow = DateUtil.addDay(now,1);

        param.put("itemType1Id",itemTypeId);
        param.put("sortDesc","commentCount");
        param.put("beginPos",0);
        param.put("pageSize",10);

        List<DmItem> list = restDmItemClient.getDmItemListByMap(param);
        //需要把查询出来的数据，转换成view需要的数据
        List<HotItemVo> list2 = new ArrayList<HotItemVo>();
        if(EmptyUtils.isNotEmpty(list)){
            for(DmItem item : list){
                HotItemVo iv = new HotItemVo();
                BeanUtils.copyProperties(item,iv);//复制同名属性
                list2.add(iv);
            }
        }
        return DtoUtil.returnDataSuccess(list2);
    }


    @PostMapping("/api/p/queryItemDetail")
    public Dto queryItemDetail(@RequestBody Map<String,String> params) throws Exception {
        ItemDetailVo dv = null;
        //根据id查询item节目
        DmItem item = restDmItemClient.getDmItemById(Long.valueOf(params.get("id")));
        if(EmptyUtils.isEmpty(item)){
            return null;
        }
        dv = new ItemDetailVo();
        BeanUtils.copyProperties(item,dv);//复制同名属性
        //根据剧院id查询剧院
        DmCinema cinema = restDmCinemaClient.getDmCinemaById(item.getCinemaId());
        dv.setAddress(cinema.getAddress());// 剧场的经纬度，
        dv.setLatitude(cinema.getLatitude());// 剧场的经纬度，
        dv.setLongitude(cinema.getLongitude());// 剧场的地址
        //查询地区
        DmArea area = restDmAreaClient.getDmAreaById(cinema.getAreaId());
        dv.setAreaId(area.getId());//区域id
        dv.setAreaName(area.getName());//区域name
        //查询海报图片
        List<DmImage> images = restDmImageClient.queryDmImageList(item.getId(),2,1);
        if(EmptyUtils.isNotEmpty(images)) {
            dv.setImgUrl(images.get(0).getImgUrl());
        }

        return DtoUtil.returnDataSuccess(dv);
    }


}
